package com.javafactroy.kunlong.domain.generalquery.repository;

import com.javafactroy.kunlong.base.config.datasource.JdbcTemplateFactroy;
import com.javafactroy.kunlong.base.exception.BusinessException;
import com.javafactroy.kunlong.base.model.ReturnPageInfo;
import com.javafactroy.kunlong.data.convert.QueryConditionApiConvert;
import com.javafactroy.kunlong.data.dto.QueryConditionApiDto;
import com.javafactroy.kunlong.data.dto.QueryMasterApiDto;
import com.javafactroy.kunlong.data.entity.QueryConditionApiEntity;
import com.javafactroy.kunlong.data.entity.QueryMasterApiEntity;
import com.javafactroy.kunlong.data.service.QueryConditionService;
import com.javafactroy.kunlong.data.service.QueryMasterService;
import com.javafactroy.kunlong.domain.generalquery.agg.QueryMasterAgg;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
@Slf4j
public class QueryMasterAggRepository {
    @Autowired
    JdbcTemplateFactroy jdbcTemplateFactroy;


    @Autowired
    QueryConditionApiConvert queryConditionApiConvert;

    @Autowired
    QueryMasterService queryMasterService;

    @Autowired
    QueryConditionService queryConditionService;

    public ReturnPageInfo<QueryMasterApiEntity> loadByAppId(Long appid){
        ReturnPageInfo<QueryMasterApiEntity> returnPageInfo = queryMasterService.query(new QueryMasterApiDto().setMasterId(appid));

        return returnPageInfo;
    }
    public QueryMasterAgg load(Long masterId) {
        QueryMasterApiEntity entity = queryMasterService.selectByPkey(masterId);
        if (entity == null) {
            throw new BusinessException(500, "配置的查询不存在！");
        }
        log.info("master=\r\n{}", CommonUtils.toJSONString(entity));
        ReturnPageInfo<QueryConditionApiEntity> pi = queryConditionService.query(new QueryConditionApiDto().setMasterId(masterId));
        List<QueryConditionApiDto> dtos = queryConditionApiConvert.toList(pi.getList());
        return new QueryMasterAgg().setMasterId(masterId).setQueryMasterApiEntity(entity).setQueryConditionApiDtos(dtos);

    }

    public void saveMasterSql(QueryMasterAgg agg) {
        queryMasterService.updateNotNull(new QueryMasterApiEntity().setMasterId(agg.getMasterId()));
    }

    public List<Map<String, Object>> queryForList(QueryMasterAgg agg, Map<String, Object> params) {
        agg.setQueryParams(params);
        return queryForList(agg);
    }

    public List<Map<String, Object>> queryForList(QueryMasterAgg agg) {
        String sql = agg.buildSelectSql().toString();
        log.info("select sql = \r\n{}", sql);

        return jdbcTemplateFactroy.findMysql().queryForList(sql);
    }

    public <T> List<T> query(String sql, Class<T> cls) {
        List<T> list = jdbcTemplateFactroy.findMysql().query(sql, new BeanPropertyRowMapper(cls));
        return list;
    }

    public <T> List<T> query(QueryMasterAgg agg, Class<T> cls) {
        List<T> list = jdbcTemplateFactroy.findMysql().query(agg.buildSelectSql().toString(), new BeanPropertyRowMapper(cls));
        return list;
    }
}
